﻿using System;
using System.Collections;
using System.Windows;
using System.Windows.Data;
using Project5188.BUS;
using Project5188.Client.ClientCodes;

namespace Project5188.Client
{
    /// <summary>
    /// Interaction logic for ParameterValuePage.xaml
    /// </summary>
    public partial class ParameterValuePage
    {
        private CollectionViewSource _resources;
        private long _parameterId;

        public ParameterValuePage() : base(Extensions.PageNames.ParameterValuePage)
        {
            InitializeComponent();

            pagingControl.DataChanged += PagingControlDataChanged;

            DataBind();
        }

        public ParameterValuePage(long id)
            : base(Extensions.PageNames.ParameterValuePage)
        {
            InitializeComponent();

            pagingControl.DataChanged += PagingControlDataChanged;

            DataBind(id);
        }

        private void DataBind()
        {
            var parameterValues = new ParameterValues();
            _resources = Resources["parameterValuesViewSource"] as CollectionViewSource;
            pagingControl.SetDataSource(parameterValues.GetEntityList(), 1, 10);
            if (_resources != null) _resources.Source = pagingControl.Paging.GetDataList();
            if (_resources != null) dgParameters.ItemsSource = _resources.Source as IEnumerable;
        }

        private void DataBind(long id)
        {
            _parameterId = id;
            var parameterValues = new ParameterValues();
            _resources = Resources["parameterValuesViewSource"] as CollectionViewSource;
            pagingControl.SetDataSource(parameterValues.GetEntityByParameterId(id), 1, 10);
            if (_resources != null) _resources.Source = pagingControl.Paging.GetDataList();
            if (_resources != null) dgParameters.ItemsSource = _resources.Source as IEnumerable;
        }

        private void BtnUpdateClick(object sender, RoutedEventArgs e)
        {
            var paramValue = (parametervalue)dgParameters.SelectedItem;
            var update = new UpdateWindow();
            update.Closed += UpdateClosed;
            DetailView<parametervalue> parameterValueDetail = new ParameterValues();
            update.SetEntity(parameterValueDetail, Extensions.PageNames.ParameterValuePage.ToString(), paramValue.ID, _parameterId);
            update.Show();
        }

        private void BtnDeleteClick(object sender, RoutedEventArgs e)
        {
            var paramValue = (parametervalue)dgParameters.SelectedItem;
            DetailView<parametervalue> parameterValueDetail = new ParameterValues();
            GenerateDeleteDialog(parameterValueDetail, paramValue.ID);
        }

        private void BtnDetailClick(object sender, RoutedEventArgs e)
        {
            var paramValue = (parametervalue)dgParameters.SelectedItem;
            var detail = new DetailWindow();
            DetailView<parametervalue> parameterValueDetail = new ParameterValues{Entity = paramValue};
            detail.SetEntity(parameterValueDetail, Extensions.PageNames.ParameterValuePage.ToString());
            detail.Show();
        }

        void PagingControlDataChanged(object sender, EventArgs e)
        {
            if (_resources != null) _resources.Source = pagingControl.Paging.GetDataList();
            if (_resources != null) dgParameters.ItemsSource = _resources.Source as IEnumerable;
        }

        private void BtnAddClick(object sender, RoutedEventArgs e)
        {
            var insert = new InsertWindow();
            insert.Closed += InsertClosed;
            DetailView<parametervalue> parameterValueDetail = new ParameterValues();
            insert.SetEntity(parameterValueDetail, Extensions.PageNames.ParameterValuePage.ToString(), _parameterId);
            insert.Show();
        }

        public override bool Search()
        {
            if (base.Search())
            {
                var paramValues = new ParameterValues();
                _resources = Resources["parameterValuesViewSource"] as CollectionViewSource;
                pagingControl.SetDataSource(paramValues.GetEntityListByParameters(txtParameterValueName.Text), 1, 10);
                if (_resources != null) _resources.Source = pagingControl.Paging.GetDataList();
                if (_resources != null) dgParameters.ItemsSource = _resources.Source as IEnumerable;
            }
            else if (TransactionInfo.TransactionId.HasValue)
            {
                var paramValues = new ParameterValues();
                _resources = Resources["parameterValuesViewSource"] as CollectionViewSource;
                pagingControl.SetDataSource(paramValues.GetEntityById(TransactionInfo.TransactionId.Value), 1, 10);
                if (_resources != null) _resources.Source = pagingControl.Paging.GetDataList();
                if (_resources != null) dgParameters.ItemsSource = _resources.Source as IEnumerable;
            }

            return true;
        }
    }
}
